package com.wys.mall.service.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.wys.common.exception.RRException;
import com.wys.common.utils.R;
import com.wys.mall.service.utils.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 对象存储
 *
 * @Package com.wys.mall.service.controller
 * @Author charlin
 * @Version: v1.0
 * @Date 2021-06-1721:41
 * @Copyright: 2021 www.wyscha.com Inc. All rights reserved.
 */
@RestController
@Slf4j
public class OssController {

    @Autowired
    private OSS ossClient;
    @Value("${alibaba.cloud.oss.endpoint}")
    private String endpoint;
    @Value("${alibaba.cloud.oss.bucket}")
    private String bucket;
    @Value("${alibaba.cloud.access-key}")
    private String accessId;

    @GetMapping("/oss/policy")
    public R policy() {
        try {
            Map<String, String> data = OssUtil.doGet(ossClient, endpoint, bucket, accessId);
            return R.ok().put("data", data);
        } catch (Exception e) {
            throw new RRException(e.getMessage());
        }
    }
}
